<template>
    <div>
        <div>
            <el-input
                    style="width: 350px;margin-right: 8px"
                    placeholder="添加职位"
                    prefix-icon="el-icon-plus"
                    v-model="pos.name"
                    @keydown.enter.native="addPosition">
            </el-input>
            <el-button icon="el-icon-plus" type="primary" @click="addPosition">添加</el-button>
        </div>
        <div style="padding-top: 20px">
            <el-table
                    :data="positions"
                    border
                    stripe
                    style="width: 70%"
                    @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="50">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="60">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职位名称"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="createdate"
                        label="创建时间"
                        width="150"
                        >
                </el-table-column>
                <el-table-column
                        prop="enabled"
                        label="是否启用"
                        width="150"
                        >
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag type="danger" v-if="!scope.row.enabled">未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showEditView(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button type="danger" size="small" style="margin-top: 10px" :disabled="multipleSelection==0" @click="deleteMany">批 量 删 除</el-button>
        </div>
        <el-dialog
                title="修改职位"
                :visible.sync="dialogVisible"
                width="30%">
            <el-tag>职位名称</el-tag>
            <el-input size="small" class="updatePosInput" v-model="updatePos.name"></el-input>
            <br/>
            <el-tag style="margin-right: 8px">是否启用</el-tag>
            <el-switch
                    size="small"
                    v-model="updatePos.enabled"
                    active-text="启用"
                    inactive-text="禁用">
            </el-switch>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "PosManager",
        data(){
            return {
                pos:{
                    name:''
                },
                positions: [],
                updatePos:{
                    name:'',
                    enabled:true
                },
                dialogVisible:false,
                multipleSelection: []
            }
        },
        methods:{
            initPositions(){
                this.getRequest("/system/basic/pos/").then(resp=>{
                    if (resp){
                        this.positions = resp;
                    }
                })
            },
            addPosition(){
                if (this.pos.name){
                    this.postRequest("/system/basic/pos/",this.pos).then(resp=>{
                        if (resp){
                            this.initPositions();
                            this.pos = {};
                        }
                    })
                } else {
                    this.$message.error('职位名称不能为空');
                }
            },
            showEditView(index,data){
                //this.updatePos = Object.assign({}, data);//把date放在新的对象里面
                Object.assign(this.updatePos, data);
                this.dialogVisible=true;
            },
            doUpdate(){
                this.putRequest("/system/basic/pos/",this.updatePos).then(resp=>{
                    if (resp){
                        this.initPositions();
                        this.updatePos.name='';
                    }
                    this.dialogVisible=false;
                })
            },
            handleDelete(index,data){
                this.$confirm('此操作将永久删除【'+data.name+'】职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/pos/"+data.id).then(resp=>{
                        if (resp){
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            deleteMany(){
                this.$confirm('此操作将永久删除【'+this.multipleSelection.length+'】条记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.multipleSelection.forEach(item=>{
                       ids += 'ids='+item.id+'&';
                    });
                    this.deleteRequest("/system/basic/pos/"+ids).then(resp=>{
                        if (resp){
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消批量删除'
                    });
                });
            },
            handleSelectionChange(val){
                this.multipleSelection = val;
            }
        },
        mounted() {
            this.initPositions();
        }


    }
</script>

<style>
.updatePosInput{
    width: 200px;
    margin-left: 8px;
}
</style>